<template>
  <div>
    <el-table
      ref="singleTable"
      :data="cutObj.records"
      highlight-current-row
      @current-change="handleCurrentChange"
      @row-click="handleTableRow"
      style="
        width: 660px;
        height: 260px;
        border: 1px solid #dcdfe6;
        margin: 100px;
      "
    >
      <el-table-column property="expertId" width="50"> </el-table-column>
      <el-table-column property="expertName" label="姓名" width="120">
      </el-table-column>
      <el-table-column property="expertUnit" label="工作单位" width="120">
      </el-table-column>
      <el-table-column property="expertSpecialty" label="专长" width="120">
      </el-table-column>
      <el-table-column property="expertJob" label="职务" width="120">
      </el-table-column>
      <el-table-column property="expertTel" label="电话" width="120">
      </el-table-column>
    </el-table>

    <div class="check">
      <div>
        <el-pagination
          background
          layout="prev, pager, next"
          :total="cutObj.pages * 10"
          @current-change="handleChange"
          @click="get()"
          style="margin-left: 100px; margin-bottom: 100px"
        >
        </el-pagination>

        <div style="margin: 20px 60px">
          <el-button @click="addexp()">添加专家</el-button>
          <el-button @click="getexpById(expertId)">查看专家信息</el-button>

          <br />
          <div style="margin-top: 20px">
            <el-button @click="del(expertId)">删除专家</el-button>
            <el-button @click="updatemsg(expertId)">修改专家信息</el-button>
          </div>
        </div>
      </div>
      <div>
        <div>
          <fieldset style="width: 280px">
            <legend>事件查询</legend>
            <form action="">
              专家姓名:<input
                type="text"
                v-model="expertName"
                style="margin: 20px"
              />
              <br />
              
              <span>专长:</span>
              <input
                type="text"
                v-model="expertSpecialty"
                style="margin: 20px 36px"
              />
              <br />
              工作单位:<input
                type="text"
                v-model="expertUnit"
                style="margin: 20px"
              />
              <br />
              <input
                type="button"
                value="查询"
                @click="get()"
                style="margin-left: 80px; margin-top: 10px"
              />
            </form>
          </fieldset>
        </div>
      </div>
    </div>

    <el-dialog
      title=""
      :visible.sync="dialogVisible"
      width="600px"
      :before-close="handleClose"
    >
      <h1 style="text-align: center">专家详细信息</h1>
      <div class="msg" style="display: flex">
        <div style="width: 300px">
          <p style="margin: 80px 30px">专家姓名：{{ expList.expertName }}</p>
          <p style="margin: 80px 30px">出生日期： {{ expList.expertBirth }}</p>
          <p style="margin: 80px 30px">性别：{{ expList.expertGender }}</p>
          <p style="margin: 80px 30px">专长:{{ expList.expertSpecialty }}</p>
          <p style="margin: 80px 30px">电话：{{ expList.expertTel }}</p>
          <p style="margin: 80px 30px">通讯地址：{{ expList.expertAddress}}</p>
        </div>
        <div style="width: 300px">
          <p style="margin: 50px 30px">
            <img
              :src="'/api' + expList.expertHeadpic"
              alt="哦豁，图片找不到---瓜了"
              style="width: 220px"
            />
          </p>
          <p style="margin: 50px 30px">职务：{{ expList.expertJob }}</p>
          <p style="margin: 50px 30px">工作单位： {{ expList.expertUnit }}</p>
          <p style="margin: 50px 30px">邮箱：{{ expList.expertEmail }}</p>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          @click="dialogVisible = false"
          style="margin-right: 240px; margin-bottom: 50px"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <el-dialog
      title=""
      :visible.sync="dialog"
      width="600px"
      :before-close="handleClose"
    >
      <h1 style="text-align: center">添加专家</h1>
      <div class="msg" style="display: flex">
        <div style="width: 300px">
          <p style="margin: 50px 30px">
            姓名：<input type="text" v-model="expertName" />
          </p>
          <p style="margin: 50px 30px">
            出生日期：<input type="date" v-model="expertBirth" />
          </p>
          <p style="margin: 50px 30px">
            专长：<input type="text" v-model="expertSpecialty" />
          </p>
          <p style="margin: 50px 30px">
            电话：<input type="text" v-model="expertTel" />
          </p>

          <p style="margin: 50px 30px">
            通讯地址：<input type="text" v-model="expertAddress" />
          </p>
        </div>
        <div style="width: 300px">
          <p style="margin: 50px 30px">
            性别：<input type="text" v-model="expertGender" />
          </p>
          <p style="margin: 50px 30px">
            照片：
            <el-upload
              class="avatar-uploader"
              action="/api/up/upload"
              :http-request="testUpload"
              :show-file-list="false"
              :before-upload="beforeAvatarUpload"
            >
              <img
                v-if="imageUrl"
                :src="imageUrl"
                class="avatar"
                style="width: 50px; height: 50px"
              />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </p>
          <p style="margin: 50px 30px">
            职务：<input type="text" v-model="expertJob" />
          </p>
          <p style="margin: 50px 30px">
            工作单位：<input type="text" v-model="expertUnit" />
          </p>
          <p style="margin: 50px 30px">
            邮箱：<input type="text" v-model="expertEmail" />
          </p>
        </div>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog = false">取 消</el-button>
        <el-button type="primary" @click="add()">添 加</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title=""
      :visible.sync="dia"
      width="600px"
      :before-close="handleClose"
    >
      <h1 style="text-align: center">修改专家信息</h1>
      <div class="msg" style="display: flex">
        <div style="width: 300px">
          <p style="margin: 80px 30px">专家姓名：{{ expList.expertName }}</p>
          <p style="margin: 80px 30px">出生日期： {{ expList.expertBirth }}</p>
          <p style="margin: 80px 30px">性别：{{ expList.expertGender }}</p>
          <p style="margin: 80px 30px">专长:{{ expList.expertSpecialty }}</p>
          <p style="margin: 80px 30px">电话：{{ expList.expertTel }}</p>
          <p style="margin: 80px 30px">
            
            电话：<input type="text" v-model="expertTel" />

          </p>
          <p style="margin: 80px 30px">通讯地址：{{ expList.eventAdvice }}</p>
        </div>
        <div style="width: 300px">
          <p style="margin: 50px 30px">
            <img
              :src="'/api' + expList.expertHeadpic"
              alt="哦豁，图片找不到---瓜了"
              style="width: 220px"
            />
          </p>
          <p style="margin: 50px 30px">
            职务：<input type="text" v-model="expertJob" />
          </p>
          <p style="margin: 50px 30px">
            工作单位：<input type="text" v-model="expertUnit" />
          </p>
          <p style="margin: 50px 30px">
            邮箱：<input type="text" v-model="expertEmail" />
          </p>
        </div>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          @click="up(eventId)"
          style="margin-right: 240px; margin-bottom: 50px"
          >修 改</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions } from "vuex";

export default {
  data() {
    return {
      pageNo: "",
      pages: "",
      expertId: "",
      expertName: "",
      expertGender: "",
      expertBirth: "",
      expertHeadpic: "",
      expertUnit: "",
      expertSpecialty: "",
      expertJob:"",
      expertTel: "",
      expertAddress: "",
      expertEmail: "",     
      expList: [],
      imageUrl: "",
      dialogVisible: false,
      dialog: false,
      dia: false,
    };
  },
  computed: {
    ...mapState("exp", ["cutObj", "param"]),
  },
  methods: {
    ...mapMutations("exp", ["setParam"]),
    ...mapActions("exp", ["findByItem"]),

    handleChange(index) {
      console.log(`当前页: ${index}`);
      this.pageNo = index;
      console.log("页码" + this.pageNo);
      this.cut(this.pageNo);
    },
    get(){

      let paramObj = this.param;
      console.log(this.expertName + this.expertUnit + this.expertSpecialty)
      paramObj.expertName = this.expertName;
      paramObj.expertUnit = this.expertUnit;
      paramObj.expertSpecialty = this.expertSpecialty;
      // 把最新的信息装入 store中的 param对象中
      this.setParam(paramObj);
      console.log(paramObj)
      this.findByItem();
    },
    cut(pageNo) {
      let paramObj = this.param;
      paramObj.pageNo = pageNo;
      this.setParam(paramObj);
      this.findByItem();
    },
    async getexpById(expertId) {
      let paramobj = { expertId: expertId };

      let expObj = await this.ajax.getSubmit(
         "/api/expert/findExpertById",
        paramobj
      );
      console.log(expObj);

      this.expList = expObj;

      this.dialogVisible = true;
    },

    handleCurrentChange(val) {
      this.currentRow = val;
      console.val;
    },
    handleTableRow(row) {
      console.log(row.expertId);

      this.expertId = row.expertId;
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    addexp() {
      this.dialog = true;
    },
    async testUpload(item) {
      let info = await this.ajax.uploadSubmit("/api/up/upload", {
        file: item.file,
      });
      console.log(info);
      this.imageUrl = "/api/img/" + info;

      this.expertHeadpic = "/img/" + info;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    async add() {
      console.log(this.eventImg);
      let eventobj = {
        expertName: this.expertName,
        expertGender: this.expertGender,
        expertBirth: this.expertBirth,
        expertHeadpic: this.expertHeadpic,
        expertUnit: this.expertUnit,
        expertSpecialty: this.expertSpecialty,
        expertJob: this.expertJob,
        expertTel: this.expertTel,
        expertAddress: this.expertAddress,
        expertEmail: this.expertEmail,

      };

      let Obj = await this.ajax.getSubmit("/api/expert/addExpert", eventobj);

      console.log(Obj);

      if (Obj != null) {
        alert("添加成功");
      }

      this.dialog = false;

      this.findByItem();
    },
    async del(expertId) {

      let paramobj = { expertId: expertId };

      let expObj = await this.ajax.getSubmit(
        "/api/expert/delExpert",
        paramobj
      );
      console.log(expObj);

        alert("删除成功");

        this.findByItem();

    },
    async updatemsg(expertId) {

      this.dia = true;

      let paramobj = { expertId: expertId };

      let expObj = await this.ajax.getSubmit(
         "/api/expert/findExpertById",
        paramobj
      );
      console.log(expObj);

      this.expList = expObj;
    },
    async up(eventId) {

      let paramobj = {
          eventId: eventId,
          expertTel: this.expertTel,
          expertJob: this.expertJob,
          expertUnit: this.expertUnit,
          expertEmail: this.expertEmail,
      };
      let updateObj = await this.ajax.getSubmit(
        "/api/expert/updateExpert",
        paramobj
      );
      console.log(updateObj);
      if (updateObj != null) {
        alert("修改成功");
      }
      this.dia = false;

      this.findByItem();
    },
  },
  created() {
    this.findByItem();
  },
};
</script>

<style>
.check {
  display: flex;
}
.check > div:first-child {
  width: 450px;
  height: 200px;
}

.check > div:nth-child(2) {
  width: 300px;
  height: 200px;
}
</style>